
import {staffCheck,todayOrder,todayOrderReport,orderReport } from "../../../http/api";
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userType:"",
    staffId:"",
    todayOrderCount:0,
    todayAmountPrice:0,
    allOrderCount:0,
    orderArr:[],
    send:0,
    unsend:0,
    percentage:0
  },
  toPage(e){


    //订阅消息
    app.shopSubscribeMessageNewOrder()


    
    const {url} = e.currentTarget.dataset
    
    wx.navigateTo({
      url
    })
  },
  getOrderReport(){
    wx.showLoading()
    orderReport().then(res=>{
      if(res.data.code === 200){
        const{allOrderCount,todayAmountPrice,todayOrderCount} = res.data.data
        this.setData({
          allOrderCount,todayAmountPrice,todayOrderCount
        })
      }
    }).finally(()=>{
      wx.hideLoading()
    })

  },

  getSendReport(){
    wx.showLoading()
    const params = {
      staffType:wx.getStorageSync('userType'),
      staffId:wx.getStorageSync('staffId'),
      reportType:1
    }
    todayOrderReport(params).then(res=>{
      if(res.data.code === 200){
        const {percentage,send,unsend} = res.data.data
        this.setData({
          percentage,send,unsend
        })
        
      }
    }).finally(()=>{
      wx.hideLoading()
    })
  },

  getOrder(){
    wx.showLoading()
    const params = {
      staffType:wx.getStorageSync('userType'),
      staffId:wx.getStorageSync('staffId')
    }
    todayOrder(params).then(res=>{
      if(res.data.code === 200){
        const orderArr = res.data.data
        this.setData({
          orderArr
        })
      }
    }).finally(()=>{
      wx.hideLoading()
    })
  },

  init(){
    this.getOrder();
    this.getSendReport();
    this.getOrderReport()
  },

  getUserType(){
    wx.showLoading()
    const phone = wx.getStorageSync('phone')
    staffCheck({phone}).then(res=>{
      if(res.data.code === 200){
        const {staffType,id} = res.data.data
        this.setData({
          userType:staffType,
          staffId:id
        })
        wx.setStorageSync('userType', staffType);
        wx.setStorageSync('staffId', id)
        this.init()
      }
    }).finally(()=>{
      wx.hideLoading()
    })
  },

  handlePhone(e){
    
    const {phone} = e.currentTarget.dataset

    wx.makePhoneCall({
      phoneNumber: phone //仅为示例，并非真实的电话号码
    })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    
    

    

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if(!wx.getStorageSync('userType')){
      this.getUserType()
    }else{
      this.init()
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})